import {StoreOptions} from "vuex";
import accessEnum from "@/access/accessEnum";
import {UserControllerService} from "../../generated";

export default {
    namespaced: true,
    state: () => ({
        loginUser: {
            userName: "未登录",
            userRole: accessEnum.NOT_LOGIN
        }
    }),
    actions: {
        async getLoginUser({commit, state}, payload) {
            const res = await UserControllerService.getLoginUserUsingGet();
            // console.log("res = ", res)
            if(res.code === 0) {
                // console.log("用户？？", res.data);
                commit("updateUser", res.data);
            }else {
                commit('updateUser', {
                    ...state.loginUser,
                    userRole: accessEnum.NOT_LOGIN
                })
            }
        }
    },

    mutations: {
        updateUser(state, payload) {
            console.log("store.mutation被调用，用户数据变化", payload)
            state.loginUser = payload;
        }
    }
}as StoreOptions<any>
